﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using CommerceOne.Common;
using CommerceOne.Domains.IRepositories;
using CommerceOne.Domains.IRepositoties;
using CommerceOne.Domains.SqlRepositories;
using CommerceOne.Models;
using CommerceOne.Models.Entities;
using CommerceOne.Widgets.CheckoutStep;

namespace CommerceOne.Controllers
{
    public class CartController : ControllerBase
    {
        private readonly IProductRepository _productRepository = new SqlProductRepository();

        //private readonly ICountryRepository _countryRepository = new SqlCountryRepository();
        //private readonly ICartRepository _cartRepository = new SqlCartRepository();
        //private readonly IUserRepository _userRepository = new SqlUserRepository();

        /// <summary>
        /// Indexes the specified cart.
        /// </summary>
        /// <param name="cart">The cart.</param>
        /// <returns></returns>
        public ActionResult Index(Cart cart)
        {
            var cartViewModel = new CartViewModel();
            var parameter = new Parameters() {ObjectId = 0, PageType = PageType.Cart};
            this.CommonInitialize(cartViewModel, parameter);

            var checkoutStepController = new CheckoutStepController() { HttpContextBase = this.HttpContext };
            cartViewModel.Step = checkoutStepController.GetViewModel(parameter.PageType);

            string returnUrl = Request.QueryString["returnUrl"].ToString();
            cartViewModel.ReturnUrl = returnUrl;

            cartViewModel.Cart = cart;
            cartViewModel.LastProduct = cart.GetLastProduct();

            return View(cartViewModel);
        }

        /// <summary>
        /// Adds to cart.
        /// </summary>
        /// <param name="cart">The cart.</param>
        /// <param name="id">The id.</param>
        /// <param name="qty">The qty.</param>
        /// <param name="returnUrl">The return URL.</param>
        /// <returns></returns>
        public RedirectToRouteResult AddToCart(Cart cart, int id, int qty,string returnUrl)
        {
            Product product = this._productRepository.GetProductById(id);
            if (product != null) cart.Add(product, qty);

            if (String.IsNullOrEmpty(returnUrl)) returnUrl = Request.QueryString["returnUrl"].ToString();
           return RedirectToAction("Index", new { returnUrl = returnUrl });
        }

        /// <summary>
        /// Removes the specified cart.
        /// </summary>
        /// <param name="cart">The cart.</param>
        /// <param name="id">The id.</param>
        /// <param name="returnUrl">The return URL.</param>
        /// <returns></returns>
        public RedirectToRouteResult Remove(Cart cart, int id, string returnUrl)
        {
            if (String.IsNullOrEmpty(returnUrl)) returnUrl = Request.QueryString["returnUrl"].ToString();

            Product product = this._productRepository.GetProductById(id);
            if (product != null) cart.Remove(product);
            return RedirectToAction("Index", new { returnUrl });
        }

        ///// <summary>
        ///// Summeries the specified cart.
        ///// </summary>
        ///// <param name="cart">The cart.</param>
        ///// <returns></returns>
        //public PartialViewResult Summery(Cart cart)
        //{
        //    return PartialView(cart);
        //}

        ///// <summary>
        ///// Checks the out.
        ///// </summary>
        ///// <returns></returns>
        //public ActionResult CheckOut()
        //{
        //    //CheckOutViewModel viewModel = new CheckOutViewModel();

        //    //this.CommonInitialize(viewModel);

        //    //var user =  this._userRepository.GetUser(5);
        //    //viewModel.Cart = new Domains.Entities.Cart();
        //    //viewModel.Cart.Phone1 = user.Phone1;
        //    //viewModel.Cart.Phone2 = user.Phone2;
        //    //viewModel.Cart.ReceiptName = user.Name + " " + user.LastName;
        //    //viewModel.Cart.ShippingAddress = user.Address;
        //    //viewModel.Cart.ShippingCity = user.City;
        //    //viewModel.Cart.ShippingCountry = user.Country.ToString();
        //    //viewModel.Cart.CellPhone = user.CellPhone;

        //    //// Get Country
        //    //viewModel.Countries = this._countryRepository.All();

        //    //return View(viewModel);

        //    return View();
        //}

        //[HttpPost]
        //public ActionResult CheckOut(Cart cart, CheckOutViewModel viewModel)
        //{
        //    //Domains.Entities.Cart shippingCart = viewModel.Cart;
        //    //shippingCart.UserId = 5;
        //    //shippingCart.Status = "N";
        //    //shippingCart.CheckoutDate = DateTime.Now;
        //    //shippingCart.CartDetails = new List<CartDetail>();

        //    //foreach (var product in cart.ProductLine)
        //    //{
        //    //    CartDetail cartDetail = new CartDetail();
        //    //    cartDetail.Price = product.Price;
        //    //    cartDetail.ProductId = product.Id;
        //    //    cartDetail.ProductName = product.Name;
        //    //    cartDetail.Vol = product.Quantity;
        //    //    cartDetail.Total = product.SubTotal;
        //    //    shippingCart.CartDetails.Add(cartDetail);
        //    //}

        //    //int result = this._cartRepository.Add(shippingCart);

        //    //if (result > 0) 
        //    //{
        //    //    cart.ClearCart();
        //    //    return RedirectToAction("CheckOutThankYou");
        //    //}

        //    //return View(viewModel);

        //    return View();
        //}

        //public ActionResult CheckOutThankYou()
        //{
        //    //CheckOutThankYouViewModel viewModel = new CheckOutThankYouViewModel();
        //    //this.CommonInitialize(viewModel);
        //    //return View(viewModel);
        //    return View();
        //}
    }
}
